package com.lanxin.hr.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lanxin.hr.entity.*;
import com.lanxin.hr.mapper.ZzAccountMapper;
import com.lanxin.hr.mapper.ZzEmployeeMapper;
import com.lanxin.hr.mapper.ZzExprienceMapper;
import com.lanxin.hr.mapper.ZzJobinfoMapper;
import com.lanxin.hr.service.IZzAccountService;
import com.lanxin.hr.service.IZzEmployeeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lanxin.hr.service.IZzExprienceService;
import com.lanxin.hr.service.IZzJobinfoService;
import com.lanxin.hr.vo.EmpVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cyh
 * @since 2025-07-16
 */
@Service
public class ZzEmployeeServiceImpl extends ServiceImpl<ZzEmployeeMapper, ZzEmployee> implements IZzEmployeeService {

    @Autowired
    private ZzEmployeeMapper employeeMapper;

    @Autowired
    private IZzAccountService accountService;

    @Autowired
    private ZzAccountMapper accountMapper;

    @Autowired
    private IZzJobinfoService jobinfoService;

    @Autowired
    private ZzJobinfoMapper jobinfoMapper;

    @Autowired
    private ZzExprienceMapper exprienceMapper;

    @Autowired
    private IZzExprienceService exprienceService;

    //根据部门id和职位id查询下属员工,用于添加部门经理
    @Override
    public List<ZzEmployee> listEmpUseSelect(Integer deptid, Integer jobid) {
        QueryWrapper<ZzEmployee> queryWrapper = new QueryWrapper<ZzEmployee>();
        if(deptid!=null){
            queryWrapper.eq("deptid", deptid);
        }
        if(jobid!=null){
            queryWrapper.eq("jobid", jobid);
        }
        return employeeMapper.selectList(queryWrapper);
    }

    //查询部门下的员工
    @Override
    public List<ZzEmployee> listEmpByDid(Integer did) {
        QueryWrapper<ZzEmployee> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deptid",did);
        return employeeMapper.selectList(queryWrapper);
    }

    //根据条件分页查询员工信息
    @Override
    public IPage<EmpVo> listEmpVo(ZzEmployee employee, Integer page, Integer pageSize) {
        if(page == null || page < 1){
            page = 1;
        }
        if(pageSize == null ){
            pageSize = 5;
        }

        Page<EmpVo> empVoPage = new Page<>(page, pageSize);
        IPage<EmpVo> empVoIPage = employeeMapper.listEmpVo(empVoPage, employee);
        return empVoIPage;
    }

    //添加员工信息及账号等信息
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public int addEmp(ZzEmployee employee, ZzAccount account, ZzJobinfo jobinfo,List<ZzExprience> experiences) {
        try {
            if(employee!=null){
                //添加员工信息，名称一样也可以添加，id不同即可
                employeeMapper.insert(employee);// 插入后ID会自动回填到实体对象中
                //得到刚才插入的员工的id
                Integer eid=employee.getOid();
                //员工id和账号id为外键关联
                account.setAid(eid);
                account.setInactive("Y");
                accountMapper.insert(account);
                //根据员工id添加工作信息
                jobinfo.setEmpid(eid);
                jobinfoMapper.insert(jobinfo);
                //添加实习经历
                if(experiences != null && !experiences.isEmpty()){
                    for (ZzExprience experience : experiences) {
                        experience.setEmpid(eid);
                        exprienceMapper.insert(experience);
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
        return 1;
    }

    //根据员工id查询EmpVo
    @Override
    public EmpVo getEmpVoByEid(Integer eid) {
        return employeeMapper.getEmpVoByEid(eid);
    }

    //更新员工信息及账号，工作，工作经验
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public int updateEmp(ZzEmployee employee,ZzAccount account,ZzJobinfo jobinfo,List<ZzExprience> experiences) {
        try {
            //更新员工信息
            employeeMapper.updateById(employee);
            //更新账号
            accountService.updateAccountById(account);
            //更新工作信息
            jobinfoService.updateJobinfoById(jobinfo);
            //更新工作经验
            if(experiences != null && !experiences.isEmpty()){
                exprienceService.updateExprienceById(employee.getOid(),experiences);
            }
        }catch (Exception e){
            return 0;
        }
        return 1;
    }

    //更新员工状态
    @Override
    public boolean updateEmpStatus(Integer eid, String state) {
        Map<String,Object> map = new HashMap<>();
        map.put("oid",eid);
        map.put("state",state);
        int row = employeeMapper.updateEmpStatus(map);
        if(row>0){
            return true;
        }
        return false;
    }


    //删除员工
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public boolean deleteEmp(Integer eid) {
        try {
            //删除工作信息
            jobinfoService.deleteJobinfoById(eid);
            //删除账号信息
            accountService.deleteAccountById(eid);
            //删除工作经验
            exprienceService.deleteExperienceById(eid);
            //删除员工信息,员工信息最后删除，因为员工id是其他表的外键。
            int row = employeeMapper.deleteById(eid);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }


    //修改员工的部门和职位
    @Override
    public int adjustEmp(ZzAdjustment adjustment) {
        return employeeMapper.adjustEmp(adjustment);
    }

    //根据id查询员工信息
    @Override
    public ZzEmployee getEmpById(Integer eid) {
        return employeeMapper.selectById(eid);
    }

}
